package com.shop.cereshop.business.dl.logic.service.Impl;

import com.shop.cereshop.business.dao.dl.TicketingImportDao;
import com.shop.cereshop.business.dl.logic.domian.CareShopNews;
import com.shop.cereshop.business.page.product.ShopProduct;
import com.shop.cereshop.business.param.product.*;
import com.shop.cereshop.business.service.product.CereShopProductService;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.domain.image.Image;
import com.shop.cereshop.commons.exception.CoBusinessException;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @description
 * @Author YF
 * @Date 日期:2021/6/16 时间:16:36
 */
@Service
public class AddDataImpl {

    @Autowired
    private TicketingImportDao ticketingImportDao;
    @Autowired
    private CereShopProductService cereShopProductService;

    //.商品分类序号
    @Value("${Dl_Message.classifyId}")
    private Long classifyId;

    //.商品分组序号
    @Value("${Dl_Message.shopGroupId}")
    private Long shopGroupId;

    //.供应商序号
    @Value("${Dl_Message.supplierId}")
    private Long supplierId;

    //.供应商名称
    @Value("${Dl_Message.supplierName}")
    private String supplierName;

    //.是否需要物流()
    @Value("${Dl_Message.ifLogistics}")
    private String ifLogistics;

    //.是否允许上架
    @Value("${Dl_Message.shelveState}")
    private String shelveState;

    //.是否允许超卖
    @Value("${Dl_Message.ifOversold}")
    private String ifOversold;

    //.设置关联店铺
    @Value("${Dl_Message.shopId}")
    private Long shopId;


    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void AddList(List list, CerePlatformBusiness user) throws CoBusinessException {
        //1.循环遍历productList集合
        for (Object o1 : list) {
            //2.转成Json格式参数
            JSONObject jsonObject = JSONObject.fromObject(o1);
            //3.得到所有productNo
            String productNo = (String) jsonObject.get("productNo");
            //4.得到所有的产品名称
            String productName = (String) jsonObject.get("productName");
            //5.得到所有的retailPrice门市价
            Double retailPrice = Double.valueOf(String.valueOf((Integer) jsonObject.get("retailPrice")));
            //6.得到所有的contractPrice协议价
            Double contractPrice = Double.valueOf(String.valueOf(jsonObject.get("contractPrice")));
            //7.得到所有的productIntroduction产品简介
            String productIntroduction = (String) jsonObject.get("productIntroduction");
            //8.得到所有的productDesc产品详情
            String productDesc = (String) jsonObject.get("productDesc");
            //.得到费用包含
            String feeInclude = (String) jsonObject.get("feeInclude");
            //.得到费用不含
            String feeExclude = (String) jsonObject.get("feeExclude");
            //.得到景区换票地址
            String getTicketLocation = (String) jsonObject.get("getTicketLocation");
            //.得到营业时间
            String openDate = (String) jsonObject.get("openDate");

            //9.得到图片集合
            ArrayList<Image> arrayList = new ArrayList<>();
            //10.循环遍历10次读取图片属性
            for (int i = 1; i <= 10; i++) {
                if (jsonObject.get("img" + i) != null) {
                    Image image = new Image();
                    //11.循环遍历，得到所有的图片
                    image.setImgPath((String) jsonObject.get("img" + i));
                    arrayList.add(image);
                }
            }
            //12.创建添加商品实体类
            ProductSaveParam saveParam = new ProductSaveParam();
            //13.设置产品名称
            saveParam.setProductName(productName);
            //14.设置产品简介
            saveParam.setProductBrief(productIntroduction);
            //15.设置产品富文本
            saveParam.setProductText(productDesc);
            //16.设置产品分类序号
            saveParam.setClassifyId(classifyId);
            //17.设置商品分组序号
            saveParam.setShopGroupId(shopGroupId);
            //18.设置供应商序号
            saveParam.setSupplierId(supplierId);
            //19.设置供应商名称
            saveParam.setSupplierName(supplierName);
            //20.设置是否需要物流
            saveParam.setIfLogistics(ifLogistics);
            //21.设置是否需要上架
            saveParam.setShelveState(shelveState);
            //22.设置是否允许超卖
            saveParam.setIfOversold(ifOversold);
            //23.设置关联店铺
            saveParam.setShopId(shopId);
            //24.设置商品平台产品编号
            saveParam.setProductNo(productNo);
            //25.设置图片
            saveParam.setImages(arrayList);
            //.设置费用包含
            saveParam.setFeeInclude(feeInclude);
            //.设置费用不含
            saveParam.setFeeExclude(feeExclude);
            //.设置景区换票地址
            saveParam.setGetTicketLocation(getTicketLocation);
            //.设置营业时间
            saveParam.setOpenDate(openDate);
            //26.设置Names里的属性
            ArrayList<SkuNameParam> names = new ArrayList<>();
            ArrayList<SkuValueParam> skuValueParams = new ArrayList<>();
            SkuNameParam skuNameParam = new SkuNameParam();
            skuNameParam.setNeedImg(false);
            SkuValueParam param = new SkuValueParam();
            param.setSkuValue("抚州文旅票务");
            skuValueParams.add(param);
            skuNameParam.setValues(skuValueParams);
            names.add(skuNameParam);
            //27.设置Names
            saveParam.setNames(names);
            //28.设置skus里的属性
            ArrayList<SkuParam> skuParams = new ArrayList<>();
            SkuParam skuParam = new SkuParam();
            skuParam.setSku("20");
            skuParam.setPrice(new BigDecimal(contractPrice).divide(BigDecimal.valueOf(100)));
            skuParam.setOriginalPrice(new BigDecimal(retailPrice).divide(BigDecimal.valueOf(100)));
            skuParam.setStockNumber(999);
            skuParam.setStyle(0);
            skuParam.setWeight(new BigDecimal(0));
            skuParams.add(skuParam);
            //29.设置skus
            saveParam.setSkus(skuParams);
            //14.根据productNo查询数据库是否有这张表
            int productCount = ticketingImportDao.selectShopProductCount(productNo);
            //15.如果有这张表则是修改，没有则是添加
            if (productCount != 0) {
                //.根据产品编号去查询产品数据并返回
                ShopProduct shopProduct = ticketingImportDao.selectShopProduct(productNo);
                ProductDeleteParam deleteParam = new ProductDeleteParam();
                deleteParam.setProductId(shopProduct.getProductId());
                //.先根据ProductId查询资讯
                List<CareShopNews> careShopNews = ticketingImportDao.getCereShopNews(shopProduct.getProductId());
                //.再根据ProductId修改资讯关联表newsId = null
                for (CareShopNews careShopNew : careShopNews) {
                    ticketingImportDao.updateCereShopNewsColumn(careShopNew.getNewsId());
                    //.再更改资讯表的参数
                    ticketingImportDao.updateCereShopNews(careShopNew.getNewsId());
                }
                //.根据商品序号删除
                cereShopProductService.delete(deleteParam,user);
                //============修改（先删除再新增）===========//
                cereShopProductService.save(saveParam,user);
            } else if (productCount <= 0) {
                //==============添加=============//
                cereShopProductService.save(saveParam, user);
            }

            //添加日志

        }
    }


    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = {CoBusinessException.class, Exception.class})
    public void AddData(Object result, CerePlatformBusiness user) throws CoBusinessException {
        //2.转成Json格式参数
        JSONObject jsonObject = JSONObject.fromObject(result);
        //3.得到所有productNo
        String productNo = (String) jsonObject.get("productNo");
        //4.得到所有的产品名称
        String productName = (String) jsonObject.get("productName");
        //5.得到所有的retailPrice门市价
        Double retailPrice = Double.valueOf(String.valueOf((Integer) jsonObject.get("retailPrice")));
        //6.得到所有的contractPrice协议价
        Double contractPrice = Double.valueOf(String.valueOf(jsonObject.get("contractPrice")));
        //7.得到所有的productIntroduction产品简介
        String productIntroduction = (String) jsonObject.get("productIntroduction");
        //8.得到所有的productDesc产品详情
        String productDesc = (String) jsonObject.get("productDesc");
        //.得到费用包含
        String feeInclude = (String) jsonObject.get("feeInclude");
        //.得到费用不含
        String feeExclude = (String) jsonObject.get("feeExclude");
        //.得到景区换票地址
        String getTicketLocation = (String) jsonObject.get("getTicketLocation");
        //.得到营业时间
        String openDate = (String) jsonObject.get("openDate");
        //9.得到图片集合
        ArrayList<Image> arrayList = new ArrayList<>();
        //10.循环遍历10次读取图片属性
        for (int i = 1; i <= 10; i++) {
            if (jsonObject.get("img" + i) != null) {
                Image image = new Image();
                //11.循环遍历，得到所有的图片
                image.setImgPath((String) jsonObject.get("img" + i));
                arrayList.add(image);
            }
        }
        //12.创建添加商品实体类
        ProductSaveParam saveParam = new ProductSaveParam();
        //13.设置产品名称
        saveParam.setProductName(productName);
        //14.设置产品简介
        saveParam.setProductBrief(productIntroduction);
        //15.设置产品富文本
        saveParam.setProductText(productDesc);
        //16.设置产品分类序号
        saveParam.setClassifyId(classifyId);
        //17.设置商品分组序号
        saveParam.setShopGroupId(shopGroupId);
        //18.设置供应商序号
        saveParam.setSupplierId(supplierId);
        //19.设置供应商名称
        saveParam.setSupplierName(supplierName);
        //20.设置是否需要物流
        saveParam.setIfLogistics(ifLogistics);
        //21.设置是否需要上架
        saveParam.setShelveState(shelveState);
        //22.设置是否允许超卖
        saveParam.setIfOversold(ifOversold);
        //23.设置关联店铺
        saveParam.setShopId(shopId);
        //24.设置商品平台产品编号
        saveParam.setProductNo(productNo);
        //25.设置图片
        saveParam.setImages(arrayList);
        //.设置费用包含
        saveParam.setFeeInclude(feeInclude);
        //.设置费用不含
        saveParam.setFeeExclude(feeExclude);
        //.设置景区换票地址
        saveParam.setGetTicketLocation(getTicketLocation);
        //.设置营业时间
        saveParam.setOpenDate(openDate);
        //26.设置Names里的属性
        ArrayList<SkuNameParam> names = new ArrayList<>();
        ArrayList<SkuValueParam> skuValueParams = new ArrayList<>();
        SkuNameParam skuNameParam = new SkuNameParam();
        skuNameParam.setNeedImg(false);
        SkuValueParam param = new SkuValueParam();
        param.setSkuValue("单人票");
        skuValueParams.add(param);
        skuNameParam.setValues(skuValueParams);
        names.add(skuNameParam);
        //27.设置Names
        saveParam.setNames(names);
        //28.设置skus里的属性
        ArrayList<SkuParam> skuParams = new ArrayList<>();
        SkuParam skuParam = new SkuParam();
        skuParam.setSku("20");
        skuParam.setPrice(new BigDecimal(contractPrice));
        skuParam.setOriginalPrice(new BigDecimal(retailPrice));
        skuParam.setStockNumber(999);
        skuParam.setStyle(0);
        skuParam.setWeight(new BigDecimal(0));
        skuParams.add(skuParam);
        //29.设置skus
        saveParam.setSkus(skuParams);
        //14.根据productNo查询数据库是否有这张表
        int productCount = ticketingImportDao.selectShopProductCount(productNo);
        //15.如果有这张表则是修改，没有则是添加
        if (productCount != 0) {
            //.根据产品编号去查询产品数据并返回
            ShopProduct shopProduct = ticketingImportDao.selectShopProduct(productNo);
            ProductDeleteParam deleteParam = new ProductDeleteParam();
            deleteParam.setProductId(shopProduct.getProductId());
            //.根据商品序号删除
            cereShopProductService.delete(deleteParam,user);
            //============修改（先删除再新增）===========//
            cereShopProductService.save(saveParam,user);
        } else if (productCount <= 0) {
            //==============添加=============//
            cereShopProductService.save(saveParam, user);
        }

    }




}

